package com.heima.schedule.feign;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dtos.Task;
import com.heima.schedule.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ScheduleClient {
    @Autowired
    private TaskService taskService;
    /**
     * 添加任务
     *
     * @param task 任务
     * @return 任务id
     */
    @PostMapping("/api/v1/task/add")
    public ResponseResult addTask(@RequestBody Task task) {
        long taskId = taskService.addTask(task);
        return ResponseResult.okResult(taskId);
    }

    /**
     * 取消任务
     *
     * @param taskId
     */
    @GetMapping("/api/v1/task/cancel/{taskId}")
    public ResponseResult cancelTask(@PathVariable Long taskId) {
        boolean flag = taskService.cancelTask(taskId);
        return ResponseResult.okResult(flag);
    }

    /**
     * 消费任务
     * @param taskType 任务类型
     * @param priority 任务优先级
     * @return 任务对象
     */
    @GetMapping("/api/v1/task/cancel/{taskType}/{priority}")
    public ResponseResult poll(@PathVariable Integer taskType, @PathVariable Integer priority) {
        Task task = taskService.poll(taskType, priority);
        return ResponseResult.okResult(task);
    }
}
